/**
 * 
 */
package gov.army.workshop.util;

import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.h2.tools.Server;

/**
 * Utility to start the HSQL server.
 * 
 * @author Denis Pavlov
 * @since 1.0.0
 * 
 */
public final class H2ServerUtil {

    private static final Logger LOG = Logger.getLogger(H2ServerUtil.class);

    private static final H2ServerUtil UTIL = new H2ServerUtil();
    private Server h2Server;

    private H2ServerUtil() {
        // prevent instantiation
    }

    /**
     * @return utility instance.
     */
    public static H2ServerUtil getInstance() {
        return UTIL;
    }

    private void doStart() throws SQLException {

        h2Server = Server.createPgServer().start();
        LOG.info("HSQLDB server started on port " + h2Server.getPort() + "...");
    }

   

    /**
     * start the server with a database configuration.
     * 
     * @param dbName
     *            the name of database
     * @throws SQLException 
     */
    public void start(final String dbName) throws SQLException {      
        doStart();
    }

    /**
     * shutdown the started instance.
     */
    public void stop() {
        LOG.info("HSQLDB server shutting down...");
        h2Server.stop();
        LOG.info("HSQLDB server shutting down... done");
    }

}
